-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
29 lines (24 loc) · 846 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def min_coins(coins, amount):
# Sort coins in descending order
coins.sort(reverse=True)
count = 0
used_coins = []
for coin in coins:
while amount >= coin:
amount -= coin
count += 1
used_coins.append(coin)
return count, used_coins, amount
if __name__ == "__main__":
n = int(input("Enter the number of coin denominations: "))
coins = []
print("Enter the coin denominations:")
for _ in range(n):
coins.append(int(input()))
amount = int(input("Enter the amount: "))
count, used_coins, remainder = min_coins(coins, amount)
if remainder != 0:
print("Exact amount cannot be formed with the given denominations.")
else:
print("Coins used:", used_coins)
print("Minimum number of coins needed:", count)